import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testconfigmap:

    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/configmap/1_get_configmap_list.yaml'))
    def test_get_configmap_list(self, configmapinfo, get_env_info):
        # 获取configmap列表接口
        baseUrl = get_env_info
        url = baseUrl + configmapinfo['request']['url'].replace('{namespace}','default')
        method = configmapinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/configmap/2_create_configmap.yaml'))
    def test_create_configmap(self, configmapinfo, get_env_info):
        # 创建configmap接口
        baseUrl = get_env_info
        url = baseUrl + configmapinfo['request']['url'].replace('{namespace}','default')
        method = configmapinfo['request']['method']
        data = configmapinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '不正确' not in configmapinfo['name']:
            configmapname=req.json()['metadata']['name']
            globals()['configmapname']=configmapname
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/configmap/3_get_specific_configmap.yaml'))
    def test_get_specific_configmap(self, configmapinfo, get_env_info):
        # 获取指定configmap接口
        baseUrl = get_env_info
        url = baseUrl + configmapinfo['request']['url'].replace('{namespace}','default').replace('{configmapname}',globals()['configmapname'])
        method = configmapinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '不正确' not in configmapinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/configmap/4_update_configmap.yaml'))
    def test_update_configmap(self, configmapinfo, get_env_info):
        # 更新指定configmap接口
        baseUrl = get_env_info
        url = baseUrl + configmapinfo['request']['url'].replace('{namespace}','default').replace('{configmapname}',globals()['configmapname'])
        method = configmapinfo['request']['method']
        configmapinfo['request']['data'] = globals()['updatereqbody']
        data = configmapinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/configmap/5_update_section_configmap.yaml'))
    def test_update_section_configmap(self, configmapinfo, get_env_info):
        # 部分更新指定configmap的接口
        baseUrl = get_env_info
        url = baseUrl + configmapinfo['request']['url'].replace('{namespace}','default').replace('{configmapname}',globals()['configmapname'])
        method = configmapinfo['request']['method']
        data = configmapinfo['request']['data']
        header = configmapinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/configmap/6_delete_specific_configmap.yaml'))
    def test_delete_specific_configmap(self, configmapinfo, get_env_info):
        # 删除configmap接口
        baseUrl = get_env_info
        url = baseUrl + configmapinfo['request']['url'].replace('{namespace}','default').replace('{configmapname}',globals()['configmapname'])
        method = configmapinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']